//
//  WBComposePictureCollectionView.swift
//  Weibo
//
//  Created by xu on 2017/3/28.
//  Copyright © 2017年 xuantong. All rights reserved.
//

import UIKit

class WBComposePictureCollectionView: UICollectionView {
    
    
    var imagesArray:[UIImage] = [UIImage]()
    
    // 打开相册回调

    var colsure:(()->())?
    
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
       
        
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.itemSize = CGSize(width: cellWH, height: cellWH)
        flowLayout.minimumLineSpacing = 5
        flowLayout.minimumInteritemSpacing = 5
         super.init(frame: frame, collectionViewLayout: flowLayout)
        
        setupUI()


    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: 供外界调用的方法
    func addImage(image: UIImage){
        // 显示配图
        isHidden = false
               // 添加到数组中
        imagesArray.insert(image, at: 0)
        // 只能保存9张图片 如果大于9张 删除最后一张
        if imagesArray.count == 9 {
            imagesArray.removeLast()
        }
        // 刷新
        reloadData()
    }
    

    
    
    private func setupUI() {

        register(HMComposePictureViewCell.self, forCellWithReuseIdentifier: "cell")
        dataSource = self
        delegate = self
        isHidden = true

        self.backgroundColor = UIColor.orange
        
    }
    
}



extension WBComposePictureCollectionView:UICollectionViewDelegate,UICollectionViewDataSource {
   
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        let count = imagesArray.count
        if count == 0 || count == 9 {
            
            return count
        }
        
        return count+1
    }
    
   
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! HMComposePictureViewCell
    
        cell.backgroundColor = UIColor.red
        if indexPath.item == imagesArray.count {
             cell.image = nil
        }else{
            
            cell.image = imagesArray[indexPath.item]
            //点击删除按钮移除图片
            cell.closure = {[weak self] in
                self?.imagesArray.remove(at: indexPath.item)
                // 如果没有图片了 需要隐藏配图
                if self?.imagesArray.count == 0 {
                self?.isHidden = true

                }

                self?.reloadData()
              }

         

        }
        
       return cell
    }
    

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        collectionView.deselectItem(at: indexPath, animated: true)
        // 判断如果是加号图片点击了
        if indexPath.item == imagesArray.count {
            colsure?()
        }
    }

}


class HMComposePictureViewCell: UICollectionViewCell {

     // 供外界赋值的属性
    var image:UIImage?{
        
    didSet{
        if image == nil {
        imgView.image =  UIImage(named: "compose_pic_add")
        imgView.highlightedImage =  UIImage(named: "compose_pic_add_highlighted")
            // 隐藏删除按钮
        closeButton.isHidden = true

        }else{
           imgView.image = image
           imgView.highlightedImage = nil
            // 显示删除按钮
            closeButton.isHidden = false
        }
       
     }
    
 }

    
    // 告知外界删除对应的图片
    var closure:(()->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    private func setupUI() {
       
        contentView.addSubview(imgView)
        contentView.addSubview(closeButton)

        imgView.snp.makeConstraints { (make) in
             make.edges.equalTo(contentView)
        }
        closeButton.snp.makeConstraints { (make) in
             make.top.right.equalTo(contentView)
        }
    }
    
    
    
    //懒加载
    
    private lazy var imgView:UIImageView = UIImageView()

    private lazy var closeButton: UIButton = {
        
        let button = UIButton()
        button.setImage(UIImage(named:"compose_photo_close"), for: UIControlState.normal)
        
        button.addTarget(self, action: #selector(buttonClick), for: UIControlEvents.touchUpInside)
        
        
        return button
    }()
}

extension HMComposePictureViewCell{
    
    //按钮点击实现方法
    @objc fileprivate func buttonClick(){
        closure?()
    }

    
}
